package cn.yza.算法.Leetcode;

/**
 * @author Honglixi
 * @create 2022-01-16 15:14
 */
public class Leetcode543 {
    public static void main(String[] args) {
//        TreeNode treeNode = new TreeNode(1,2,3);
        
    }
    int maxDiameter = 0;
    public int diameterOfBinaryTree(TreeNode root) {
        maxDepth(root);
        return maxDiameter;
    }
    private int maxDepth(TreeNode root) {
        if (root == null) {
            return 0;
        }
        int leftMax = maxDepth(root.left);
        int rightMax = maxDepth(root.right);
        maxDiameter = Math.max(maxDiameter,leftMax + rightMax);
        return Math.max(leftMax, rightMax) + 1;
    }
}
class TreeNode {
    int val;
    TreeNode left;
    TreeNode right;

    public TreeNode() {
    }

    public TreeNode(int val) {
        this.val = val;
    }

    public TreeNode(int val, TreeNode left, TreeNode right) {
        this.val = val;
        this.left = left;
        this.right = right;
    }
}